package main;

public class GameFlow {
	
	Controller cont;
	Choice user;
	Choice opponent;
	boolean userSet;
	boolean opponentSet;
	int round;
	int userScore;
	int opponentScore;
	
	public GameFlow(Controller cont)
	{
		this.cont = cont;
		reset();
	}
	
	
	/**
	 * Reset all game data
	 */
	public void reset(){
		user = null;
		opponent = null;
		userSet = false;
		opponentSet = false;
		round = 1;
		userScore = 0;
		opponentScore = 0;
	}
	
	/**
	 * User made his choice - check that it has not already been done this round.
	 * If opponent also has chosen - call roundFinished()
	 * @param user
	 */
	public void userChoice(Choice user){
		if (user != null && !userSet){
			this.user = user;
			userSet = true;
			if (opponentSet){
				roundFinished();
			}
		}
	}
	/**
	 * See userChoice()
	 * @param opponent
	 */
	public void opponentChoice(Choice opponent){
		if (opponent != null && !opponentSet){
			this.opponent = opponent;
			opponentSet = true;
			if (userSet){
				roundFinished();
			}
		}
	}
	
	/**
	 * Set scores and create messages for view according to user and opponent choices.
	 */
	private void roundFinished()
	{
		userSet = false;
		opponentSet = false;
		String msg;
		if (user.compareTo(opponent) == 1){
			userScore++;
			msg = "Round " + round + ": Opponent chose " + opponent.toString() + ". Your point!";
		} else if (user.compareTo(opponent) == -1){
			opponentScore++;
			msg = "Round " + round + " Opponent chose " + opponent.toString() + ". You lose...";
		} else {
			msg = "Round " + round + " Opponent chose " + opponent.toString() + ". Draw.";
		}
		round++;
		cont.roundFinished(msg, userScore, opponentScore);
	}
}
